#! /bin/sh

if [ "$packagename" == "" ]; then
    project="${TRAVIS_REPO_SLUG##*/}"
else
    project="$packagename"
fi
if [ "$gh_version" == "True" ]; then
    package="$project"_"$TRAVIS_TAG".unitypackage
else
    package=$project.unitypackage
fi

echo "------------------------------------------------------------------------------------------------------------------------"
echo "Setting up project directory; ------------------------------------------------------------------------------------------"
echo "------------------------------------------------------------------------------------------------------------------------"
mkdir ./Project
if [ "$verbose" == "True" ];
then
    /Applications/Unity/Unity.app/Contents/MacOS/Unity \
     -batchmode \
     -nographics \
     -silent-crashes \
     -logFile ./.deploy/unityProject.log \
     -createProject ./Project \
     -quit
    
    echo "------------------------------------------------------------------------------------------------------------------------"
    echo 'Project Log; -----------------------------------------------------------------------------------------------------------'
    echo "------------------------------------------------------------------------------------------------------------------------"
    cat ./.deploy/unityProject.log
    echo "------------------------------------------------------------------------------------------------------------------------"
    echo "------------------------------------------------------------------------------------------------------------------------"
    echo "------------------------------------------------------------------------------------------------------------------------"
else
    /Applications/Unity/Unity.app/Contents/MacOS/Unity \
     -batchmode \
     -nographics \
     -silent-crashes \
     -createProject ./Project \
     -quit
fi

echo "------------------------------------------------------------------------------------------------------------------------"
echo "Moving files into temporary project; -----------------------------------------------------------------------------------"
echo "------------------------------------------------------------------------------------------------------------------------"
mkdir -p ./Project/Assets/"$project"
if [ "$verbose" == "True" ];
then
    find ./* \
     ! -path '*/\.*' \
     ! -path "./Project/*" \
     ! -name "Project" \
     ! -path "./.deploy/*" \
     ! -name ".deploy" \
     ! -name ".gitignore" \
     -exec rsync -Rv {} ./Project/Assets/"$project"/ \;
else
    find ./* \
     ! -path '*/\.*' \
     ! -path "./Project/*" \
     ! -name "Project" \
     ! -path "./.deploy/*" \
     ! -name ".deploy" \
     ! -name ".gitignore" \
     -exec rsync -R {} ./Project/Assets/"$project"/ \;
fi

echo "------------------------------------------------------------------------------------------------------------------------"
echo "Attempting to package $project;"
echo "------------------------------------------------------------------------------------------------------------------------"
if [ "$verbose" == "True" ];
then
    /Applications/Unity/Unity.app/Contents/MacOS/Unity \
     -batchmode \
     -nographics \
     -silent-crashes \
     -logFile ./.deploy/unityPackage.log \
     -projectPath "$PWD"/Project \
     -exportPackage Assets/"$project" "$package" \
     -quit
     
    echo "------------------------------------------------------------------------------------------------------------------------"
    echo 'Packaging Log; ---------------------------------------------------------------------------------------------------------'
    echo "------------------------------------------------------------------------------------------------------------------------"
    cat ./.deploy/unityPackage.log
    echo "------------------------------------------------------------------------------------------------------------------------"
    echo "------------------------------------------------------------------------------------------------------------------------"
    echo "------------------------------------------------------------------------------------------------------------------------"
else
    /Applications/Unity/Unity.app/Contents/MacOS/Unity \
     -batchmode \
     -nographics \
     -silent-crashes \
     -projectPath "$PWD"/Project \
     -exportPackage Assets/"$project" "$package" \
     -quit
fi

#The package is exported to ./Project/$package
echo "------------------------------------------------------------------------------------------------------------------------"
echo "Checking package exists; -----------------------------------------------------------------------------------------------"
echo "------------------------------------------------------------------------------------------------------------------------"
file=./Project/$package

if [ -e "$file" ];
then
    echo "------------------------------------------------------------------------------------------------------------------------"
	echo "Package exported successfully: $file"
    echo "------------------------------------------------------------------------------------------------------------------------"
	exit 0
else
    echo "------------------------------------------------------------------------------------------------------------------------"
	echo "Package not exported. Aborting.----------------------------------------------------------------------------------------------"
    echo "------------------------------------------------------------------------------------------------------------------------"
	exit 1
fi
